<?

/*
	Document manager handling for users in dbi database

	This module users dbi drivers for php which are available
	at http://freshmeat.net/projects/php-dbi/
	or http://pil.dk/downloads/

	arguments in docman.conf file are:

	$dbi = "driver:database_name:user:password";
	$dbi_sql = "select login,full_name,password,email from users";

	driver can be: pgsql, mysql, oracle or odbc (as supported by php-dbi)
	SQL query must return login, password full_name and e-mail

	This file is included early in docman.php and it should return:
	$gblUserName	descriptive username
	$gblPw		md5 hash of joint login and password
	$gblEmail	e-mail address of user
*/

	// split configuration var $dbi
	// 0:driver, 1:db, 2:user, 3:password
	$dbi_arr=explode(":",$dbi);

	$dbi_class="$gblIncDir/dbi/dbi_".$dbi_arr[0].".php";
	if (file_exists($dbi_class)) {
		include($dbi_class);
	} else {
		Error("Configuration error","Can't find dbi classes <tt>$dbi_class</tt> needed for htusers_sql. Please download them from <a href=http://pil.dk/downloads/>http://pil.dk/downloads/</a>");
	}

	$pgdbi=new DBI($dbi_arr[1], $dbi_arr[2], $dbi_arr[3]);
	$sth=$pgdbi->prepare("$dbi_sql");
	$sth->execute();
	while ($row=$sth->fetchrow_array()) {
		if ($row[0] == $GLOBALS["PHP_AUTH_USER"]) {
			$gblUserName=$row[1];
			$gblPw=$row[2];
			if (substr($gblPw,0,5) == "auth_" && file_exists("$gblIncDir/$gblPw.php")) {
				require("$gblIncDir/$gblPw.php");
				if ($gblPw($row)) {
					$gblPw=md5($PHP_AUTH_USER.$PHP_AUTH_PW);
				} else {
					$gblPw="error".md5($PHP_AUTH_USER.$PHP_AUTH_PW);
				}
			}
			$gblEmail=$row[3];
			continue ;
		}
	}
	$sth->finish();

?>
